package com.zdp.controller;

import com.zdp.common.domain.ResultInfo;
import com.zdp.common.enums.OrderStatusEnum;
import com.zdp.common.enums.PayMethod;
import com.zdp.common.util.CookieUtils;
import com.zdp.common.util.JsonUtils;
import com.zdp.common.util.ResultInfoUtil;
import com.zdp.config.RedisOperator;
import com.zdp.pojo.OrderStatus;
import com.zdp.pojo.bo.ShopcartBO;
import com.zdp.pojo.bo.SubmitOrderBO;
import com.zdp.pojo.vo.OrderVO;
import com.zdp.service.OrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author sesshomaru
 * @date 2021/7/31 19:07
 * 订单相关
 */
@RequestMapping("orders")
@RestController
public class OrdersController extends BaseController{

    @Autowired
    private OrderService orderService;

    @Autowired
    private RedisOperator redisOperator;

    @PostMapping("/create")
    public ResultInfo create(@RequestBody SubmitOrderBO submitOrderBO,
                             HttpServletRequest request,
                             HttpServletResponse response) {

        // 0.接收到创建订单的BO
        System.out.println(submitOrderBO.toString());

        if (submitOrderBO.getPayMethod() != PayMethod.WEIXIN.type
                && submitOrderBO.getPayMethod() != PayMethod.ALIPAY.type ) {
            return ResultInfoUtil.buildError("支付方式不支持！");
        }

        // 创建订单之前先验证购物车中的数据是否存在
        String shopcartJson = redisOperator.get(FOODIE_SHOPCART + ":" + submitOrderBO.getUserId());
        if (StringUtils.isBlank(shopcartJson)) {
            return ResultInfoUtil.buildError("购物车数据不正确");
        }
        // 从缓存中获取到的购物车所有的信息
        List<ShopcartBO> shopcartList = JsonUtils.jsonToList(shopcartJson, ShopcartBO.class);

        // 1. 创建订单
        OrderVO order = orderService.createOrder(submitOrderBO, shopcartList);

        // 2. 创建订单以后，移除购物车中已结算（已提交）的商品
        // 清理覆盖现有的redis中的购物车数据
        shopcartList.removeAll(order.getToBeRemovedShopCartdList());
        redisOperator.set(FOODIE_SHOPCART + ":" + submitOrderBO.getUserId(), JsonUtils.objectToJson(shopcartList));
        // 整合redis之后，完善购物车中的已结算商品清除，并且同步到前端的cookie
        CookieUtils.setCookie(request, response, FOODIE_SHOPCART, JsonUtils.objectToJson(shopcartList), true);

        //TODO 3. 向支付中心发送当前订单，用于保存支付中心的订单数据 (并获取支付相关的核心参数)

        return ResultInfoUtil.buildSuccess(order.getOrderId());

    }


    // 提供给支付中心支付成功后修改订单状态为已付款
    @PostMapping("notifyMerchantOrderPaid")
    public Integer notifyMerchantOrderPaid(String merchantOrderId) {
        orderService.updateOrderStatus(merchantOrderId, OrderStatusEnum.WAIT_DELIVER.type);
        return HttpStatus.OK.value();
    }

    @PostMapping("getPaidOrderInfo")
    public ResultInfo getPaidOrderInfo(String orderId) {

        OrderStatus orderStatus = orderService.queryOrderStatusInfo(orderId);
        return ResultInfoUtil.buildSuccess(orderStatus);
    }
}
